package cn.ds.assets.module.assets.service.metadataDatasource;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.time.LocalDateTime;

import cn.ds.assets.module.assets.controller.admin.metadataDatasource.vo.*;
import cn.ds.assets.module.assets.dal.dataobject.metadataDatasource.BaseMetadataDatasourceDO;
import cn.ds.assets.framework.common.pojo.PageResult;

import cn.ds.assets.module.assets.convert.metadataDatasource.BaseMetadataDatasourceConvert;
import cn.ds.assets.module.assets.dal.mysql.metadataDatasource.BaseMetadataDatasourceMapper;

import static cn.ds.assets.framework.common.exception.util.ServiceExceptionUtil.exception;
import cn.ds.assets.framework.common.exception.enums.GlobalErrorCodeConstants;
//import static cn.ds.assets.module.assets.enums.ErrorCodeConstants.*;

/**
 * 元数据管理-数据源 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class BaseMetadataDatasourceServiceImpl implements BaseMetadataDatasourceService {

    @Resource
    private BaseMetadataDatasourceMapper baseMetadataDatasourceMapper;

    @Override
    public Long createBaseMetadataDatasource(BaseMetadataDatasourceCreateReqVO createReqVO) {
        // 插入
        BaseMetadataDatasourceDO baseMetadataDatasource = BaseMetadataDatasourceConvert.INSTANCE.convert(createReqVO);
        baseMetadataDatasourceMapper.insert(baseMetadataDatasource);
        // 返回
        return baseMetadataDatasource.getId();
    }

    @Override
    public void updateBaseMetadataDatasource(BaseMetadataDatasourceUpdateReqVO updateReqVO) {
        // 校验存在
        validateBaseMetadataDatasourceExists(updateReqVO.getId());
        // 更新
        BaseMetadataDatasourceDO updateObj = BaseMetadataDatasourceConvert.INSTANCE.convert(updateReqVO);
        baseMetadataDatasourceMapper.updateById(updateObj);
    }

    @Override
    public void deleteBaseMetadataDatasource(Long id) {
        // 校验是否存在
        validateBaseMetadataDatasourceExists(id);
        // 逻辑删除
        baseMetadataDatasourceMapper.updateToDelete(id, LocalDateTime.now());
    }

    private void validateBaseMetadataDatasourceExists(Long id) {
        if (id == null) {
            throw exception(GlobalErrorCodeConstants.ID_NOT_FOUND);
        }
        BaseMetadataDatasourceDO dataDo = baseMetadataDatasourceMapper.selectById(id);
        if (dataDo == null) {
            throw exception(GlobalErrorCodeConstants.DATA_NOT_FOUND);
        }
    }

    @Override
    public BaseMetadataDatasourceDO getBaseMetadataDatasource(Long id) {
        return baseMetadataDatasourceMapper.selectById(id);
    }

    @Override
    public List<BaseMetadataDatasourceDO> getBaseMetadataDatasourceList(Collection<Long> ids) {
        return baseMetadataDatasourceMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BaseMetadataDatasourceDO> getBaseMetadataDatasourcePage(BaseMetadataDatasourcePageReqVO pageReqVO) {
        return baseMetadataDatasourceMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BaseMetadataDatasourceDO> getBaseMetadataDatasourceList(BaseMetadataDatasourceExportReqVO exportReqVO) {
        return baseMetadataDatasourceMapper.selectList(exportReqVO);
    }

}
